import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _regeneratorRuntime from "@babel/runtime/regenerator";
var _excluded = ["tenantId", "unitId", "positionCode", "positionName", "expandFlag", "expandedRowKeys"];

/**
 * model 岗位维护与管理
 * @date: 2018-6-19


 */
import { getResponse } from 'utils/utils';
import { renderTreeData, search, gainInfo, saveAdd, saveEdit as _saveEdit, enabledLine as _enabledLine, forbindLine as _forbindLine } from "../services/postService";
export default {
  namespace: 'post',
  state: {
    pathMap: {},
    addData: {},
    renderTree: [],
    unitCode: '',
    unitName: '',
    companyId: '',
    expandedRowKeys: []
  },
  effects: {
    // 获取部门信息
    fetchUnitInfo: /*#__PURE__*/_regeneratorRuntime.mark(function fetchUnitInfo(_ref, _ref2) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function fetchUnitInfo$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.t0 = getResponse;
              _context.next = 5;
              return call(gainInfo, payload);

            case 5:
              _context.t1 = _context.sent;
              result = (0, _context.t0)(_context.t1);

              if (!result) {
                _context.next = 10;
                break;
              }

              _context.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  unitCode: result.unitCode,
                  unitName: result.unitName,
                  companyId: result.unitCompanyId
                }
              });

            case 10:
            case "end":
              return _context.stop();
          }
        }
      }, fetchUnitInfo);
    }),
    // 获取岗位信息
    fetchPositionData: /*#__PURE__*/_regeneratorRuntime.mark(function fetchPositionData(_ref3, _ref4) {
      var payload, call, put, tenantId, unitId, positionCode, positionName, expandFlag, expandedRowKeys, others, expandedRow, result, _renderTreeData, renderTree, pathMap, positionIdList;

      return _regeneratorRuntime.wrap(function fetchPositionData$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              tenantId = payload.tenantId, unitId = payload.unitId, positionCode = payload.positionCode, positionName = payload.positionName, expandFlag = payload.expandFlag, expandedRowKeys = payload.expandedRowKeys, others = _objectWithoutProperties(payload, _excluded);
              expandedRow = [];
              _context2.next = 6;
              return call(search, {
                tenantId: tenantId,
                unitId: unitId,
                positionCode: positionCode,
                positionName: positionName
              });

            case 6:
              result = _context2.sent;
              result = getResponse(result);
              _renderTreeData = renderTreeData(result, {}), renderTree = _renderTreeData.renderTree, pathMap = _renderTreeData.pathMap;

              if (!expandFlag) {
                expandedRow = {
                  expandedRowKeys: Object.keys(pathMap).map(function (item) {
                    return +item;
                  })
                };
              } else {
                positionIdList = result.map(function (item) {
                  return item.positionId;
                }) || [];
                expandedRow = {
                  expandedRowKeys: Array.from(new Set([].concat(_toConsumableArray(expandedRowKeys), _toConsumableArray(positionIdList))))
                };
              }

              _context2.next = 12;
              return put({
                type: 'updateState',
                payload: _objectSpread(_objectSpread({
                  addData: {},
                  renderTree: renderTree,
                  pathMap: pathMap
                }, expandedRow), others)
              });

            case 12:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchPositionData);
    }),
    // 新增岗位信息
    saveData: /*#__PURE__*/_regeneratorRuntime.mark(function saveData(_ref5, _ref6) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function saveData$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context3.next = 4;
              return call(saveAdd, payload);

            case 4:
              result = _context3.sent;
              return _context3.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, saveData);
    }),
    // 更新岗位信息
    saveEdit: /*#__PURE__*/_regeneratorRuntime.mark(function saveEdit(_ref7, _ref8) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function saveEdit$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              _context4.next = 4;
              return call(_saveEdit, payload);

            case 4:
              result = _context4.sent;
              return _context4.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, saveEdit);
    }),
    // 禁用岗位
    forbindLine: /*#__PURE__*/_regeneratorRuntime.mark(function forbindLine(_ref9, _ref10) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function forbindLine$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call;
              _context5.next = 4;
              return call(_forbindLine, payload);

            case 4:
              result = _context5.sent;
              return _context5.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, forbindLine);
    }),
    // 启用岗位
    enabledLine: /*#__PURE__*/_regeneratorRuntime.mark(function enabledLine(_ref11, _ref12) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function enabledLine$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref11.payload;
              call = _ref12.call;
              _context6.next = 4;
              return call(_enabledLine, payload);

            case 4:
              result = _context6.sent;
              return _context6.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context6.stop();
          }
        }
      }, enabledLine);
    })
  },
  reducers: {
    // 更新state
    updateState: function updateState(state, _ref13) {
      var payload = _ref13.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};